$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + '/accounts/config/list',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'configId', index: "id", width: 110, hidden:true, key: true },
            { label: '参数名称', name: 'configName', width: 45 },
            { label: '参数编码', name: 'configCode', width: 45 },
            { label: '参数值', name: 'configValue', width: 90 }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.result",
            page: "page.pageNo",
            total: "page.totalPages",
            records: "page.total"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});



var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            code: null
        },
        showList: true,
        configAdd: false,
        isOpen:null,
        title:null,
        config:{
            configName:null,
            configCode:null,
            configValue:null
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.configAdd = true;
            vm.title = "新增";
            vm.config = {configName:null,configCode:null,configValue:null};
        },
        update: function () {
            var grid = $("#jqGrid");
            var configId = grid.getGridParam("selrow");
            if(!configId){
                alert("请选择一条记录");
                return ;
            }
            var configDate = grid.getRowData(configId);
            if (isNumber(configDate.configValue)) {
                vm.isOpen = true;
            }else{
                vm.isOpen = false;
            }

            vm.showList = false;
            vm.configAdd = false;
            vm.title = "修改";

            vm.getConfig(configId);
        },
        del: function () {
            var configIds = getSelectedRows();
            if(configIds == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "accounts/config/delete",
                    contentType: "application/json",
                    data: JSON.stringify(configIds),
                    success: function(r){
                        if(r.code === 'success'){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.message);
                        }
                    }
                });
            });
        },
        getConfig: function(configId){
            $.get(baseURL + "accounts/config/info/"+configId, function(r){
                vm.config = r.config;
            });
        },
        saveOrUpdate: function () {
            var url = vm.config.configId == null ? "accounts/config/save" : "accounts/config/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.config),
                success: function(r){
                    if(r.code === 'success'){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.message);
                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'code': vm.q.code},
                page:page
            }).trigger("reloadGrid");
        }
    }
});

/**
 * 判断数值类型，包括整数和浮点数
 */
function isNumber(str){
    if(isDouble(str) || isInteger(str)) return true;
    return false;
}
/**
 * 匹配integer
 */
function isInteger(str){
    if(str==null||str=="") return false;
    var result=str.match(/^[-\+]?\d+$/);
    if(result==null)return false;
    return true;
}

/**
 * 匹配double或float
 */
function isDouble(str){
    if(str==null||str=="") return false;
    var result=str.match(/^[-\+]?\d+(\.\d+)?$/);
    if(result==null)return false;
    return true;
}